package Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import javabean.Admin;
import javabean.User;
import service.MainModuleService;
import serviceimpl.MainModuleServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    public static final MainModuleService mainModuleService = new MainModuleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession();
        //
        User u = (User) session.getAttribute("user");
        //System.out.println("user:" + u);
        //System.out.println("----------------");
        Admin a = (Admin) session.getAttribute("admin");
        //System.out.println("admin:" + a);
        //System.out.println("----------------");
        String time = (String) session.getAttribute("time");
        //System.out.println("time:" + time);
        //System.out.println("----------------");
        Boolean mm = (Boolean) session.getAttribute("mm");
        //System.out.println("mm:" + mm);
        //System.out.println("----------------");
        String newTime = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss").format(new Date());
        //System.out.println("newTime:" + newTime);
        //System.out.println("----------------");
        System.out.println("login登录");

        ObjectMapper js = new ObjectMapper();
        Map<String, Object> map = new HashMap<>();

        if(mm != null && mm) { //免密用户直接登录
            map.put("login", true);
            session.setAttribute("time", newTime);
            //欢迎用户登录成功的信息
            String redirectUrl = null;
            if(a != null) {
                redirectUrl = "admin.html";
            } else {
                redirectUrl = "user.html";
            }
            //欢迎用户登录成功的信息
            map.put("redirectUrl", redirectUrl);
        } else {
            map.put("login", false);
            //欢迎用户登录成功的信息
            String msg = "请登录";
            map.put("msg", msg);
        }
        resp.getWriter().println(js.writeValueAsString(map));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession();
        //获取相关表单提交参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String type = req.getParameter("type");
        Boolean mm = Boolean.valueOf(req.getParameter("mm"));
        Boolean savePassword = Boolean.valueOf("savePassword");

        System.out.println("type:" + type);
        System.out.println("username:" + username);
        System.out.println("password:" + password);
        System.out.println("mm:" + mm);
        System.out.println("savePassword:" + savePassword);
        System.out.println("------------------");

        String newTime = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss").format(new Date());

        User u = null;
        Admin a = null;
        if(!"".equals(username) && username != null){ // 获取登录信息
            if("user".equals(type)) {
                u = mainModuleService.userLogin(username, password);
            } else {
                a = mainModuleService.adminLogin(username, password);
            }
        }

        ObjectMapper js = new ObjectMapper();
        Map<String, Object> map = new HashMap<>();
        if (u != null || a != null) { // 如果登录成功，即u中有登录人员的数据，则进行相关处理
            map.put("login", true);
            Cookie uname = new Cookie("username", username);
            uname.setPath("/homework0826");
            Cookie pwd = new Cookie("password", password);
            pwd.setPath("/homework0826");
            Cookie tp = new Cookie("type", type);
            tp.setPath("/homework0826");
            Cookie spwd = new Cookie("savePassword", savePassword.toString());
            spwd.setPath("/homework0826");
            if(savePassword) {
                uname.setMaxAge(7*24*60*60);
                pwd.setMaxAge(7*24*60*60);
                tp.setMaxAge(7*24*60*60);
                spwd.setMaxAge(7*24*60*60);
            } else {
                uname.setMaxAge(0);
                pwd.setMaxAge(0);
                tp.setMaxAge(0);
                spwd.setMaxAge(0);
            } //保存密码判断 cookie
            resp.addCookie(uname);
            resp.addCookie(pwd);
            resp.addCookie(tp);
            resp.addCookie(spwd);

            if(a != null) { //只要登录就把相关参数存入
                session.setAttribute("admin", a);
            } else {
                session.setAttribute("user", u);
            };
            //System.out.println("mm: " + mm);
            //System.out.println("------------------------");
            if (mm) { //免密参数的处理
                session.setAttribute("time", newTime);
                session.setAttribute("mm", mm);
            } else {
                session.removeAttribute("time");
                session.removeAttribute("mm");
            } //免密登录判断 session

            String redirectUrl = null;
            if(a != null) {
                redirectUrl = "admin.html";
            } else {
                redirectUrl = "user.html";
            }
            //欢迎用户登录成功的信息
            map.put("redirectUrl", redirectUrl);
            System.out.println("用户或管理员正常登录");
            System.out.println("跳转至url：" + redirectUrl);
        } else {
            map.put("login", false);
            //欢迎用户登录成功的信息
            String msg = "账号或密码错误!";
            map.put("msg", msg);
        }
        resp.getWriter().println(js.writeValueAsString(map));
    }

}
